<?php
namespace Shop;
// Add these import statements:
use Shop\Model;
use Zend\View\Model\ViewModel;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\Authentication\Storage;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;
use Zend\Session\Config\SessionConfig;
use Zend\Session\SessionManager;
use Zend\Session\Container;
use Zend\Mvc\ModuleRouteListener;
use Zend\Debug\Debug;

class Module
{
    public function onBootstrap($e)
    { 
        $session = $e->getApplication()
                     ->getServiceManager()
                     ->get('Zend\Session\SessionManager');
        $session->start();
    }

	public function getServiceConfig()
	{
		return array(
			'factories'=>array(
				'Shop\Model\MyAuthStorage' => function($sm){
					return new \Shop\Model\MyAuthStorage('zf2');
				},
				'DbTableAuthAdapter' => function($sm){
					//My assumption, you've alredy set dbAdapter
					//and has users table with columns : user_name and pass_word
					//that password hashed with md5
					$dbAdapter  			= $sm->get('Zend\Db\Adapter\Adapter');
					$dbTableAuthAdapter  	= new DbTableAuthAdapter($dbAdapter,'users','username','password');
					return $dbTableAuthAdapter;
				},	 
				'AuthService' => function($sm) {					
					$authService = new AuthenticationService();
					$authService->setAdapter($sm->get('DbTableAuthAdapter'));
					$authService->setStorage($sm->get('Shop\Model\MyAuthStorage'));
					return $authService;
				},
				
				'Zend\Session\SessionManager' => function ($sm) {
					$config = $sm->get('config');
					if (isset($config['session'])) {
						$session = $config['session'];
				
						$sessionConfig = null;
						if (isset($session['config'])) {
							$class = isset($session['config']['class'])  ? $session['config']['class'] : 'Zend\Session\Config\SessionConfig';
							$options = isset($session['config']['options']) ? $session['config']['options'] : array();
							$sessionConfig = new $class();
							$sessionConfig->setOptions($options);
						}
				
						$sessionStorage = null;
						if (isset($session['storage'])) {
							$class = $session['storage'];
							$sessionStorage = new $class();
						}
				
						$sessionSaveHandler = null;
						if (isset($session['save_handler'])) {
							// class should be fetched from service manager since it will require constructor arguments
							$sessionSaveHandler = $sm->get($session['save_handler']);
						}
				
						$sessionManager = new SessionManager($sessionConfig, $sessionStorage, $sessionSaveHandler);
				
						if (isset($session['validator'])) {
							$chain = $sessionManager->getValidatorChain();
							foreach ($session['validator'] as $validator) {
								$validator = new $validator();
								$chain->attach('session.validate', array($validator, 'isValid'));
				
							}
						}
					} else {
						$sessionManager = new SessionManager();
					}
					Container::setDefaultManager($sessionManager);
					return $sessionManager;
				},
			),
		);
	}
	
	
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }
    
}
